Health R coding club

Is speechiness more important than danceability?

What made a hit record in 2017 according to spotify?


Hint: First things first, make sure you are working in a script rather than the console, to open a new script you can use the keyboard shortcut “Ctrl+ Shift +”.

Setting up our workspace

First we must set up R so that we are looking at the folder where we saved our data using the set working directory function: setwd(“…”)

Hint: Remember to flip your slashs, they should be /. This makes them r readable.

We must also we have all the packages we require installed.

install.packages("radarchart")
install.packages("ggplot2")
install.packages("kableExtra")
install.packages("tidyr")
install.packages("plotly")
install.packages("dplyr")
installed.packages("corrplot")

Reading in the data

 songs <- read.csv("spotify2017featuresdf.csv", header=T, sep=",")

Check your data

Let’s take a quick look at the data make sure it is as we expect.  There are a couple of key functions we can make use of to do this, these are summary and strusture.

str(songs)
## 'data.frame':    100 obs. of  16 variables:
##  $ id              : Factor w/ 100 levels "00lNx0OcTJrS3MKHcB80H",..: 97 62 53 84 42 93 46 41 7 49 ...
##  $ name            : Factor w/ 100 levels "1-800-273-8255",..: 73 21 22 80 40 39 47 94 89 41 ...
##  $ artists         : Factor w/ 78 levels "21 Savage","AJR",..: 24 48 48 71 20 40 42 26 8 76 ...
##  $ danceability    : num  0.825 0.694 0.66 0.617 0.609 0.904 0.64 0.726 0.853 0.735 ...
##  $ energy          : num  0.652 0.815 0.786 0.635 0.668 0.611 0.533 0.769 0.56 0.451 ...
##  $ key             : num  1 2 2 11 7 1 0 6 1 0 ...
##  $ loudness        : num  -3.18 -4.33 -4.76 -6.77 -4.28 ...
##  $ mode            : num  0 1 1 0 1 0 1 1 1 1 ...
##  $ speechiness     : num  0.0802 0.12 0.17 0.0317 0.0367 0.0888 0.0706 0.123 0.0406 0.0585 ...
##  $ acousticness    : num  0.581 0.229 0.209 0.0498 0.0552 0.000259 0.119 0.0293 0.013 0.0631 ...
##  $ instrumentalness: num  0.00 0.00 0.00 1.44e-05 0.00 2.03e-05 0.00 1.01e-02 0.00 1.30e-05 ...
##  $ liveness        : num  0.0931 0.0924 0.112 0.164 0.167 0.0976 0.0864 0.104 0.0944 0.325 ...
##  $ valence         : num  0.931 0.813 0.846 0.446 0.811 0.4 0.515 0.733 0.86 0.0862 ...
##  $ tempo           : num  96 88.9 177.8 103 80.9 ...
##  $ duration_ms     : num  233713 228827 228200 247160 288600 ...
##  $ time_signature  : num  4 4 4 4 4 4 4 4 4 4 ...

This function tells us about the structure of our data, here we can see the different variables within our data and the the variable type for example our data has numeric and factor variables.

summary(songs)
##                      id                           name   
##  00lNx0OcTJrS3MKHcB80H: 1   1-800-273-8255          : 1  
##  04DwTuZ2VBdJCCC5TROn7: 1   24K Magic               : 1  
##  0afhq8XCExXpqazXczTSv: 1   2U (feat. Justin Bieber): 1  
##  0CcQNd8CINkwQfe1RDtGV: 1   Ahora Dice              : 1  
##  0CokSRCu5hZgPxcZBaEzV: 1   All Night               : 1  
##  0dA2Mk56wEzDgegdC6R17: 1   Attention               : 1  
##  (Other)              :94   (Other)                 :94  
##              artists    danceability        energy            key       
##  Ed Sheeran      : 4   Min.   :0.2580   Min.   :0.3460   Min.   : 0.00  
##  The Chainsmokers: 4   1st Qu.:0.6350   1st Qu.:0.5565   1st Qu.: 2.00  
##  Drake           : 3   Median :0.7140   Median :0.6675   Median : 6.00  
##  Martin Garrix   : 3   Mean   :0.6968   Mean   :0.6607   Mean   : 5.57  
##  Bruno Mars      : 2   3rd Qu.:0.7702   3rd Qu.:0.7875   3rd Qu.: 9.00  
##  Calvin Harris   : 2   Max.   :0.9270   Max.   :0.9320   Max.   :11.00  
##  (Other)         :82                                                    
##     loudness            mode       speechiness       acousticness     
##  Min.   :-11.462   Min.   :0.00   Min.   :0.02320   Min.   :0.000259  
##  1st Qu.: -6.595   1st Qu.:0.00   1st Qu.:0.04312   1st Qu.:0.039100  
##  Median : -5.437   Median :1.00   Median :0.06265   Median :0.106500  
##  Mean   : -5.653   Mean   :0.58   Mean   :0.10397   Mean   :0.166306  
##  3rd Qu.: -4.327   3rd Qu.:1.00   3rd Qu.:0.12300   3rd Qu.:0.231250  
##  Max.   : -2.396   Max.   :1.00   Max.   :0.43100   Max.   :0.695000  
##                                                                       
##  instrumentalness       liveness          valence           tempo       
##  Min.   :0.000e+00   Min.   :0.04240   Min.   :0.0862   Min.   : 75.02  
##  1st Qu.:0.000e+00   1st Qu.:0.09828   1st Qu.:0.3755   1st Qu.: 99.91  
##  Median :0.000e+00   Median :0.12500   Median :0.5025   Median :112.47  
##  Mean   :4.796e-03   Mean   :0.15061   Mean   :0.5170   Mean   :119.20  
##  3rd Qu.:1.335e-05   3rd Qu.:0.17925   3rd Qu.:0.6790   3rd Qu.:137.17  
##  Max.   :2.100e-01   Max.   :0.44000   Max.   :0.9660   Max.   :199.86  
##                                                                         
##   duration_ms     time_signature
##  Min.   :165387   Min.   :3.00  
##  1st Qu.:198491   1st Qu.:4.00  
##  Median :214106   Median :4.00  
##  Mean   :218387   Mean   :3.99  
##  3rd Qu.:230543   3rd Qu.:4.00  
##  Max.   :343150   Max.   :4.00  
## 

Here we can see the ranges of numerical values. 

We can also take a look at our full data set:

library(kableExtra)
kable(songs) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), font_size = 12) %>%
scroll_box(width = "720px", height = "500px")
id name artists danceability energy key loudness mode speechiness acousticness instrumentalness liveness valence tempo duration_ms time_signature
7qiZfU4dY1lWllzX7mPBI Shape of You Ed Sheeran 0.825 0.652 1 -3.183 0 0.0802 0.581000 0.00e+00 0.0931 0.9310 95.977 233713 4
5CtI0qwDJkDQGwXD1H1cL Despacito - Remix Luis Fonsi 0.694 0.815 2 -4.328 1 0.1200 0.229000 0.00e+00 0.0924 0.8130 88.931 228827 4
4aWmUDTfIPGksMNLV2rQP Despacito (Featuring Daddy Yankee) Luis Fonsi 0.660 0.786 2 -4.757 1 0.1700 0.209000 0.00e+00 0.1120 0.8460 177.833 228200 4
6RUKPb4LETWmmr3iAEQkt Something Just Like This The Chainsmokers 0.617 0.635 11 -6.769 0 0.0317 0.049800 1.44e-05 0.1640 0.4460 103.019 247160 4
3DXncPQOG4VBw3QHh3S81 I’m the One DJ Khaled 0.609 0.668 7 -4.284 1 0.0367 0.055200 0.00e+00 0.1670 0.8110 80.924 288600 4
7KXjTSCq5nL1LoYtL7XAw HUMBLE. Kendrick Lamar 0.904 0.611 1 -6.842 0 0.0888 0.000259 2.03e-05 0.0976 0.4000 150.020 177000 4
3eR23VReFzcdmS7TYCrhC It Ain’t Me (with Selena Gomez) Kygo 0.640 0.533 0 -6.596 1 0.0706 0.119000 0.00e+00 0.0864 0.5150 99.968 220781 4
3B54sVLJ402zGa6Xm4YGN Unforgettable French Montana 0.726 0.769 6 -5.043 1 0.1230 0.029300 1.01e-02 0.1040 0.7330 97.985 233902 4
0KKkJNfGyhkQ5aFogxQAP That’s What I Like Bruno Mars 0.853 0.560 1 -4.961 1 0.0406 0.013000 0.00e+00 0.0944 0.8600 134.066 206693 4
3NdDpSvN911VPGivFlV5d I Don’t Wanna Live Forever (Fifty Shades Darker) - From “Fifty Shades Darker (Original Motion Picture Soundtrack)” ZAYN 0.735 0.451 0 -8.374 1 0.0585 0.063100 1.30e-05 0.3250 0.0862 117.973 245200 4
7GX5flRQZVHRAGd6B4TmD XO TOUR Llif3 Lil Uzi Vert 0.732 0.750 11 -6.366 0 0.2310 0.002640 0.00e+00 0.1090 0.4010 155.096 182707 4
72jbDTw1piOOj770jWNea Paris The Chainsmokers 0.653 0.658 2 -6.428 1 0.0304 0.021500 1.70e-06 0.0939 0.2190 99.990 221507 4
0dA2Mk56wEzDgegdC6R17 Stay (with Alessia Cara) Zedd 0.679 0.634 5 -5.024 0 0.0654 0.232000 0.00e+00 0.1150 0.4980 102.013 210091 4
4iLqG9SeJSnt0cSPICSjx Attention Charlie Puth 0.774 0.626 3 -4.432 0 0.0432 0.096900 3.12e-05 0.0848 0.7770 100.041 211475 4
0VgkVdmE4gld66l8iyGjg Mask Off Future 0.833 0.434 2 -8.795 1 0.4310 0.010200 2.19e-02 0.1650 0.2810 150.062 204600 4
3a1lNhkSLSkpJE4MSHpDu Congratulations Post Malone 0.627 0.812 6 -4.215 1 0.0358 0.198000 0.00e+00 0.2120 0.5040 123.071 220293 4
6kex4EBAj0WHXDKZMEJaa Swalla (feat. Nicki Minaj & Ty Dolla $ign) Jason Derulo 0.696 0.817 1 -3.862 1 0.1090 0.075000 0.00e+00 0.1870 0.7820 98.064 216409 4
6PCUP3dWmTjcTtXY02oFd Castle on the Hill Ed Sheeran 0.461 0.834 2 -4.868 1 0.0989 0.023200 1.14e-05 0.1400 0.4710 135.007 261154 4
5knuzwU65gJK7IF5yJsua Rockabye (feat. Sean Paul & Anne-Marie) Clean Bandit 0.720 0.763 9 -4.068 0 0.0523 0.406000 0.00e+00 0.1800 0.7420 101.965 251088 4
0CcQNd8CINkwQfe1RDtGV Believer Imagine Dragons 0.779 0.787 10 -4.305 0 0.1080 0.052400 0.00e+00 0.1400 0.7080 124.982 204347 4
2rb5MvYT7ZIxbKW5hfcHx Mi Gente J Balvin 0.543 0.677 11 -4.915 0 0.0993 0.014800 6.20e-06 0.1300 0.2940 103.809 189440 4
0tKcYR2II1VCQWT79i5Nr Thunder Imagine Dragons 0.600 0.810 0 -4.749 1 0.0479 0.006830 2.10e-01 0.1550 0.2980 167.880 187147 4
5uCax9HTNlzGybIStD3vD Say You Won’t Let Go James Arthur 0.358 0.557 10 -7.398 1 0.0590 0.695000 0.00e+00 0.0902 0.4940 85.043 211467 4
79cuOz3SPQTuFrp8WgftA There’s Nothing Holdin’ Me Back Shawn Mendes 0.857 0.800 2 -4.035 1 0.0583 0.381000 0.00e+00 0.0913 0.9660 121.996 199440 4
6De0lHrwBfPfrhorm9q1X Me Rehúso Danny Ocean 0.744 0.804 1 -6.327 1 0.0677 0.023100 0.00e+00 0.0494 0.4260 104.823 205715 4
6D0b04NJIKfEMg040WioJ Issues Julia Michaels 0.706 0.427 8 -6.864 1 0.0879 0.413000 0.00e+00 0.0609 0.4200 113.804 176320 4
0afhq8XCExXpqazXczTSv Galway Girl Ed Sheeran 0.624 0.876 9 -3.374 1 0.1000 0.073500 0.00e+00 0.3270 0.7810 99.943 170827 4
3ebXMykcMXOcLeJ9xZ17X Scared to Be Lonely Martin Garrix 0.584 0.540 1 -7.786 0 0.0576 0.089500 0.00e+00 0.2610 0.1950 137.972 220883 4
7BKLCZ1jbUBVqRi2FVlTV Closer The Chainsmokers 0.748 0.524 8 -5.599 1 0.0338 0.414000 0.00e+00 0.1110 0.6610 95.010 244960 4
1x5sYLZiu9r5E43kMlt9f Symphony (feat. Zara Larsson) Clean Bandit 0.707 0.629 0 -4.581 0 0.0563 0.259000 1.60e-05 0.1380 0.4570 122.863 212459 4
5GXAXm5YOmYT0kL5jHvYB I Feel It Coming The Weeknd 0.768 0.813 0 -5.940 0 0.1280 0.427000 0.00e+00 0.1020 0.5790 92.994 269187 4
5aAx2yezTd8zXrkmtKl66 Starboy The Weeknd 0.681 0.594 7 -7.028 1 0.2820 0.165000 3.50e-06 0.1340 0.5350 186.054 230453 4
1OAh8uOEOvTDqkKFsKksC Wild Thoughts DJ Khaled 0.671 0.672 0 -3.094 0 0.0688 0.032900 0.00e+00 0.1180 0.6320 97.980 204173 4
7tr2za8SQg2CI8EDgrdtN Slide Calvin Harris 0.736 0.795 1 -3.299 0 0.0545 0.498000 1.20e-06 0.2540 0.5110 104.066 230813 4
2ekn2ttSfGqwhhate0LSR New Rules Dua Lipa 0.771 0.696 9 -6.258 0 0.0755 0.002560 9.70e-06 0.1790 0.6560 116.054 208827 4
5tz69p7tJuGPeMGwNTxYu 1-800-273-8255 Logic 0.629 0.572 5 -7.733 0 0.0387 0.570000 0.00e+00 0.1920 0.3860 100.015 250173 4
7hDc8b7IXETo14hHIHdnh Passionfruit Drake 0.809 0.463 11 -11.377 1 0.0396 0.256000 8.50e-02 0.1090 0.3640 111.980 298941 4
7wGoVu4Dady5GV0Sv4UIs rockstar Post Malone 0.577 0.522 5 -6.594 0 0.0984 0.130000 9.03e-05 0.1420 0.1190 159.772 218320 4
6EpRaXYhGOB3fj4V2uDkM Strip That Down Liam Payne 0.869 0.485 6 -5.595 1 0.0545 0.246000 0.00e+00 0.0765 0.5270 106.028 204502 4
3A7qX2QjDlPnazUsRk5y0 2U (feat. Justin Bieber) David Guetta 0.548 0.650 8 -5.827 0 0.0591 0.219000 0.00e+00 0.2250 0.5570 144.937 194897 4
0tgVpDi06FyKpA1z0VMD4 Perfect Ed Sheeran 0.599 0.448 8 -6.312 1 0.0232 0.163000 0.00e+00 0.1060 0.1680 95.050 263400 3
78rIJddV4X0HkNAInEcYd Call On Me - Ryan Riback Extended Remix Starley 0.676 0.843 0 -4.068 1 0.0367 0.062300 7.52e-04 0.1810 0.7180 105.003 222041 4
5bcTCxgc7xVfSaMV3RuVk Feels Calvin Harris 0.893 0.745 11 -3.105 0 0.0571 0.064200 0.00e+00 0.0943 0.8720 101.018 223413 4
0NiXXAI876aGImAd6rTj8 Mama Jonas Blue 0.746 0.793 11 -4.209 0 0.0412 0.110000 0.00e+00 0.0528 0.5570 104.027 181615 4
0qYTZCo5Bwh1nsUFGZP3z Felices los 4 Maluma 0.755 0.789 5 -4.502 1 0.1460 0.231000 0.00e+00 0.3510 0.7370 93.973 229849 4
2EEeOnHehOozLq4aS0n6S iSpy (feat. Lil Yachty) KYLE 0.746 0.653 7 -6.745 1 0.2890 0.378000 0.00e+00 0.2290 0.6720 75.016 253107 4
152lZdxL1OR0ZMW6KquMi Location Khalid 0.736 0.449 1 -11.462 0 0.4250 0.330000 1.62e-04 0.0898 0.3260 80.126 219080 4
6mICuAdrwEjh6Y6lroV2K Chantaje Shakira 0.852 0.773 8 -2.921 0 0.0776 0.187000 3.05e-05 0.1590 0.9070 102.034 195840 4
4Km5HrUvYTaSUfiSGPJeQ Bad and Boujee (feat. Lil Uzi Vert) Migos 0.927 0.665 11 -5.313 1 0.2440 0.061000 0.00e+00 0.1230 0.1750 127.076 343150 4
0ofbQMrRDsUaVKq2mGLEA Havana Camila Cabello 0.768 0.517 7 -4.323 0 0.0312 0.186000 3.80e-05 0.1040 0.4180 104.992 216897 4
6HUnnBwYZqcED1eQztxMB Solo Dance Martin Jensen 0.744 0.836 6 -2.396 0 0.0507 0.043500 0.00e+00 0.1940 0.3600 114.965 174933 4
343YBumqHu19cGoGARUTs Fake Love Drake 0.927 0.488 9 -9.433 0 0.4200 0.108000 0.00e+00 0.1960 0.6050 133.987 210937 4
4pdPtRcBmOSQDlJ3Fk945 Let Me Love You DJ Snake 0.476 0.718 8 -5.309 1 0.0576 0.078400 1.02e-05 0.1220 0.1420 199.864 205947 4
3PEgB3fkiojxms35ntsTg More Than You Know Axwell / Ingrosso 0.644 0.743 5 -5.002 0 0.0355 0.034000 0.00e+00 0.2570 0.5440 123.074 203000 4
1xznGGDReH1oQq0xzbwXa One Dance Drake 0.791 0.619 1 -5.886 1 0.0532 0.007840 4.23e-03 0.3510 0.3710 103.989 173987 4
7nKBxz47S9SD79N086fuh SUBEME LA RADIO Enrique Iglesias 0.684 0.823 9 -3.297 0 0.0773 0.074400 0.00e+00 0.1110 0.6470 91.048 208163 4
1NDxZ7cFAo481dtYWdrUn Pretty Girl - Cheat Codes X CADE Remix Maggie Lindemann 0.703 0.868 7 -4.661 0 0.0291 0.150000 1.32e-01 0.1040 0.7330 121.030 193613 4
3m660poUr1chesgkkjQM7 Sorry Not Sorry Demi Lovato 0.704 0.633 11 -6.923 0 0.2410 0.021400 0.00e+00 0.2900 0.8630 144.021 203760 4
3kxfsdsCpFgN412fpnW85 Redbone Childish Gambino 0.743 0.359 1 -10.401 1 0.0794 0.199000 6.11e-03 0.1370 0.5870 160.083 326933 4
6b8Be6ljOzmkOmFslEb23 24K Magic Bruno Mars 0.818 0.803 1 -4.282 1 0.0797 0.034000 0.00e+00 0.1530 0.6320 106.970 225983 4
6HZILIRieu8S0iqY8kIKh DNA. Kendrick Lamar 0.637 0.514 1 -6.763 1 0.3650 0.004700 0.00e+00 0.0940 0.4020 139.931 185947 4
3umS4y3uQDkqekNjVpiRU El Amante Nicky Jam 0.683 0.691 8 -5.535 1 0.0432 0.243000 0.00e+00 0.1400 0.7320 179.910 219507 4
00lNx0OcTJrS3MKHcB80H You Don’t Know Me - Radio Edit Jax Jones 0.876 0.669 11 -6.054 0 0.1380 0.163000 0.00e+00 0.1850 0.6820 124.007 213947 4
6520aj0B4FSKGVuKNsOCO Chained To The Rhythm Katy Perry 0.448 0.801 0 -5.363 1 0.1650 0.073300 0.00e+00 0.1460 0.4620 189.798 237734 4
1louJpMmzEicAn7lzDalP No Promises (feat. Demi Lovato) Cheat Codes 0.741 0.667 10 -5.445 1 0.1340 0.057500 0.00e+00 0.1060 0.5950 112.956 223504 4
2QbFClFyhMMtiurUjuQlA Don’t Wanna Know (feat. Kendrick Lamar) Maroon 5 0.775 0.617 7 -6.166 1 0.0701 0.341000 0.00e+00 0.0985 0.4850 100.048 214265 4
5hYTyyh2odQKphUbMqc5g How Far I’ll Go - From “Moana” Alessia Cara 0.314 0.555 9 -9.601 1 0.3700 0.157000 1.08e-04 0.0670 0.1590 179.666 175517 4
38yBBH2jacvDxrznF7h08 Slow Hands Niall Horan 0.734 0.418 0 -6.678 1 0.0425 0.012900 0.00e+00 0.0579 0.8680 85.909 188174 4
2cnKEkpVUSV4wnjQiTWfH Escápate Conmigo Wisin 0.747 0.864 8 -3.181 0 0.0599 0.024500 4.46e-05 0.0853 0.7540 92.028 232787 4
0SGkqnVQo9KPytSri1H6c Bounce Back Big Sean 0.770 0.567 2 -5.698 1 0.1750 0.105000 0.00e+00 0.1250 0.2600 81.477 222360 4
5Ohxk2dO5COHF1krpoPig Sign of the Times Harry Styles 0.516 0.595 5 -4.630 1 0.0313 0.027500 0.00e+00 0.1090 0.2220 119.972 340707 4
6gBFPUFcJLzWGx4lenP6h goosebumps Travis Scott 0.841 0.728 7 -3.370 1 0.0484 0.084700 0.00e+00 0.1490 0.4300 130.049 243837 4
5Z3GHaZ6ec9bsiI5Benrb Young Dumb & Broke Khalid 0.798 0.539 1 -6.351 1 0.0421 0.199000 1.66e-05 0.1650 0.3940 136.949 202547 4
6jA8HL9i4QGzsj6fjoxp8 There for You Martin Garrix 0.611 0.644 6 -7.607 0 0.0553 0.124000 0.00e+00 0.1240 0.1300 105.969 221904 4
21TdkDRXuAB3k90ujRU1e Cold (feat. Future) Maroon 5 0.697 0.716 9 -6.288 0 0.1130 0.118000 0.00e+00 0.0424 0.5060 99.905 234308 4
7vGuf3Y35N4wmASOKLUVV Silence Marshmello 0.520 0.761 4 -3.093 1 0.0853 0.256000 5.00e-06 0.1700 0.2860 141.971 180823 4
1mXVgsBdtIVeCLJnSnmtd Too Good At Goodbyes Sam Smith 0.698 0.375 5 -8.279 1 0.0491 0.652000 0.00e+00 0.1730 0.5340 91.920 201000 4
3EmmCZoqpWOTY1g2GBwJo Just Hold On Steve Aoki 0.647 0.932 11 -3.515 1 0.0824 0.003830 1.50e-06 0.0574 0.3740 114.991 198774 4
6uFsE1JgZ20EXyU0JQZbU Look What You Made Me Do Taylor Swift 0.773 0.680 9 -6.378 0 0.1410 0.213000 1.57e-05 0.1220 0.4970 128.062 211859 4
0CokSRCu5hZgPxcZBaEzV Glorious (feat. Skylar Grey) Macklemore 0.731 0.794 0 -5.126 0 0.0522 0.032300 2.59e-05 0.1120 0.3560 139.994 220454 4
6875MeXyCW0wLyT72Eetm Starving Hailee Steinfeld 0.721 0.626 4 -4.200 1 0.1230 0.402000 0.00e+00 0.1020 0.5580 99.914 181933 4
3AEZUABDXNtecAOSC1qTf Reggaetón Lento (Bailemos) CNCO 0.761 0.838 4 -3.073 0 0.0502 0.400000 0.00e+00 0.1760 0.7100 93.974 222560 4
3E2Zh20GDCR9B1EYjfXWy Weak AJR 0.673 0.637 5 -4.518 1 0.0429 0.137000 0.00e+00 0.1840 0.6780 123.980 201160 4
4pLwZjInHj3SimIyN9SnO Side To Side Ariana Grande 0.648 0.738 6 -5.883 0 0.2470 0.040800 0.00e+00 0.2920 0.6030 159.145 226160 4
3QwBODjSEzelZyVjxPOHd Otra Vez (feat. J Balvin) Zion & Lennox 0.832 0.772 10 -5.429 1 0.1000 0.055900 4.86e-04 0.4400 0.7040 96.016 209453 4
1wjzFQodRWrPcQ0AnYnvQ I Like Me Better Lauv 0.752 0.505 9 -7.621 1 0.2530 0.535000 2.60e-06 0.1040 0.4190 91.970 197437 4
04DwTuZ2VBdJCCC5TROn7 In the Name of Love Martin Garrix 0.490 0.485 4 -6.237 0 0.0406 0.059200 0.00e+00 0.3370 0.1960 133.889 195840 4
6DNtNfH8hXkqOX1sjqmI7 Cold Water (feat. Justin Bieber & MØ) Major Lazer 0.608 0.798 6 -5.092 0 0.0432 0.073600 0.00e+00 0.1560 0.5010 92.943 185352 4
1UZOjK1BwmwWU14Erba9C Malibu Miley Cyrus 0.573 0.781 8 -6.406 1 0.0555 0.076700 2.64e-05 0.0813 0.3430 139.934 231907 4
4b4KcovePX8Ke2cLIQTLM All Night The Vamps 0.544 0.809 8 -5.098 1 0.0363 0.003800 0.00e+00 0.3230 0.4480 145.017 197640 4
1a5Yu5L18qNxVhXx38njO Hear Me Now Alok 0.789 0.442 11 -7.844 1 0.0421 0.586000 3.66e-03 0.0927 0.4500 121.971 192846 4
4c2W3VKsOFoIg2SFaO6DY Your Song Rita Ora 0.855 0.624 1 -4.093 1 0.0488 0.158000 0.00e+00 0.0513 0.9620 117.959 180757 4
22eADXu8DfOAUEDw4vU8q Ahora Dice Chris Jeday 0.708 0.693 6 -5.516 1 0.1380 0.246000 0.00e+00 0.1290 0.4270 143.965 271080 4
7nZmah2llfvLDiUjm0kiy Friends (with BloodPop®) Justin Bieber 0.744 0.739 8 -5.350 1 0.0387 0.004590 0.00e+00 0.3060 0.6490 104.990 189467 4
2fQrGHiQOvpL9UgPvtYy6 Bank Account 21 Savage 0.884 0.346 8 -8.228 0 0.3510 0.015100 7.00e-06 0.0871 0.3760 75.016 220307 4
1PSBzsahR2AKwLJgx8ehB Bad Things (with Camila Cabello) Machine Gun Kelly 0.675 0.690 2 -4.761 1 0.1320 0.210000 0.00e+00 0.2870 0.2720 137.817 239293 4
0QsvXIfqM0zZoerQfsI9l Don’t Let Me Down The Chainsmokers 0.542 0.859 11 -5.651 1 0.1970 0.160000 4.66e-03 0.1370 0.4030 159.797 208053 4
7mldq42yDuxiUNn08nvzH Body Like A Back Road Sam Hunt 0.731 0.469 5 -7.226 1 0.0326 0.463000 1.00e-06 0.1030 0.6310 98.963 165387 4
7i2DJ88J7jQ8K7zqFX2fW Now Or Never Halsey 0.658 0.588 6 -4.902 0 0.0367 0.105000 1.30e-06 0.1250 0.4340 110.075 214802 4
1j4kHkkpqZRBwE0A4CN4Y Dusk Till Dawn - Radio Edit ZAYN 0.258 0.437 11 -6.593 0 0.0390 0.101000 1.30e-06 0.1060 0.0967 180.043 239000 4

To enable us to know how popular a song is i.e. where t placed in the top 100, we must add a variable to indicate a songs position.

songs$rank <- seq.int(nrow(songs))

Feature exploration

Firstly lets look at speechiness

The Spotify Web API Guidance gives the below information on Speechiness:  

Speechiness: Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.

teal <- "#17bebb"
speech <- ggplot(songs, aes(x=reorder(name, -speechiness), y=speechiness, text=(paste( "Track:", name, "<br>", "Artist:", artists, "<br>", "Speechiness:", speechiness))))+
  geom_col(fill=teal)+
  theme_minimal()+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.ticks.y=element_blank())+
  ggtitle("Speechiness")
ggplotly(speech, tooltip=c("text"))
0.00.10.20.30.4
Speechinessspeechiness

We can now take this code and apply it to other features, for example lets look at danceability.  

Danceability: Describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.

pink <- "#ff6f59"
dance <- ggplot(songs, aes(x=reorder(name, -danceability), y=danceability, text=(paste( "Track:", name, "<br>", "Artist:", artists, "<br>", "Danceability:", danceability))))+
  geom_col(fill=pink)+
  theme_minimal()+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.ticks.y=element_blank())+
  ggtitle("Danceability")
ggplotly(dance, tooltip=c("text"))
0.000.250.500.75
Danceabilitydanceability

Finally lets take a look at key. 

Key: The key the track is in. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C???/D???, 2 = D, and so on. 

If you recall when we looked at the structure of the data this variable is slightly different as it is a factor. To help us explore it graphically we need to create a different view of the data.

songs_key <- songs%>%
  select(name, key)%>%
  mutate(n=n())%>%
  unique()%>%
  group_by(key)%>%
  mutate(total=sum(n))%>%
  mutate(percent=round((n/total)*100))

head(songs_key, 10)
## # A tibble: 10 x 5
## # Groups:   key [6]
##    name                                            key     n total percent
##    <fct>                                         <dbl> <int> <int>   <dbl>
##  1 Shape of You                                      1   100  1400       7
##  2 Despacito - Remix                                 2   100   800      12
##  3 Despacito (Featuring Daddy Yankee)                2   100   800      12
##  4 Something Just Like This                         11   100  1300       8
##  5 I'm the One                                       7   100   700      14
##  6 HUMBLE.                                           1   100  1400       7
##  7 It Ain't Me (with Selena Gomez)                   0   100  1000      10
##  8 Unforgettable                                     6   100   900      11
##  9 That's What I Like                                1   100  1400       7
## 10 "I Don’t Wanna Live Forever (Fifty Shades ~     0   100  1000      10

Lets firstly look at what percentage of all the top 100 songs are in each key.

key <- ggplot(songs_key, aes(x=key))
Relationship between features

To do this we will first just take the top 5 songs.

to make the chart more readable and clear, first we need to normalise the values from 0 to 1.

songs_5 <- songs[1:5,-c(1,3,17)]
songs_5_n <- cbind(songs_5[1], apply(songs_5[-1],2,function(x){(x-min(x))/diff(range(x))}))
radarDF <- gather(songs_5_n, key=Attribute, value=Score, -name) %>%
  spread(key=name, value=Score)

chartJSRadar(scores = radarDF,
             scaleStartValue = -1,
             maxScale=1,
             showToolTipLabel=TRUE)
songs_5 <- songs[1:5,-c(1,3,17,16)]
songs_5_n <- cbind(songs_5[1], apply(songs_5[-1],2,function(x){(x-min(x))/diff(range(x))}))
radarDF <- gather(songs_5_n, key=Attribute, value=Score, -name) %>%
  spread(key=name, value=Score)

chartJSRadar(scores = radarDF,
             scaleStartValue = -1,
             maxScale=1,
             showToolTipLabel=TRUE)

Explore relationship between features and ranking.

songs_c <- songs[,-c(1:3,16,8,15)]
pairs(songs_c)

cor(songs_c)
##                  danceability      energy          key    loudness
## danceability       1.00000000 -0.05723941 -0.068057388  0.05275867
## energy            -0.05723941  1.00000000  0.049559783  0.70914140
## key               -0.06805739  0.04955978  1.000000000 -0.05368443
## loudness           0.05275867  0.70914140 -0.053684425  1.00000000
## speechiness        0.11177472 -0.20667163  0.070721212 -0.44335872
## acousticness       0.03939829 -0.25305050  0.021401830 -0.15331960
## instrumentalness  -0.01982579  0.10112841 -0.062147726 -0.05737693
## liveness          -0.04201403  0.14774395 -0.012909244  0.06003277
## valence            0.42343467  0.32178106 -0.023195110  0.40773103
## tempo             -0.36449175  0.03267623  0.005245745 -0.13669236
## rank              -0.08687914 -0.07363648  0.222580339 -0.05735295
##                  speechiness acousticness instrumentalness      liveness
## danceability      0.11177472   0.03939829      -0.01982579 -0.0420140304
## energy           -0.20667163  -0.25305050       0.10112841  0.1477439473
## key               0.07072121   0.02140183      -0.06214773 -0.0129092438
## loudness         -0.44335872  -0.15331960      -0.05737693  0.0600327657
## speechiness       1.00000000  -0.06194163      -0.08122906 -0.0198154699
## acousticness     -0.06194163   1.00000000      -0.07425833 -0.1354167155
## instrumentalness -0.08122906  -0.07425833       1.00000000 -0.0407420087
## liveness         -0.01981547  -0.13541672      -0.04074201  1.0000000000
## valence          -0.11157553   0.12724059      -0.06152293 -0.0006445297
## tempo             0.17208244  -0.25340361       0.14818416  0.0493559990
## rank              0.03778292   0.01654014      -0.09407100  0.1048057328
##                        valence        tempo        rank
## danceability      0.4234346699 -0.364491747 -0.08687914
## energy            0.3217810590  0.032676232 -0.07363648
## key              -0.0231951095  0.005245745  0.22258034
## loudness          0.4077310289 -0.136692360 -0.05735295
## speechiness      -0.1115755271  0.172082442  0.03778292
## acousticness      0.1272405876 -0.253403611  0.01654014
## instrumentalness -0.0615229335  0.148184158 -0.09407100
## liveness         -0.0006445297  0.049355999  0.10480573
## valence           1.0000000000 -0.291877308 -0.18485703
## tempo            -0.2918773078  1.000000000  0.10012369
## rank             -0.1848570311  0.100123687  1.00000000

Next time: Clustering